package com.adjaisd.scarecrow.common.config.job.jobs;

import com.adjaisd.scarecrow.common.utils.GeoUtils;
import com.adjaisd.scarecrow.common.utils.mqtt.EmqClient;
import com.adjaisd.scarecrow.entity.Device;
import com.adjaisd.scarecrow.entity.DeviceGps;
import com.adjaisd.scarecrow.entity.DeviceStatus;
import com.adjaisd.scarecrow.service.DeviceGpsService;
import com.adjaisd.scarecrow.service.DeviceService;
import com.adjaisd.scarecrow.service.DeviceStatusService;
import com.adjaisd.scarecrow.vo.GeoCoordinate;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Component
public class MockGpsDataJob implements Job {

    @Autowired
    DeviceService deviceService;

    @Autowired
    DeviceStatusService deviceStatusService;

    @Autowired
    DeviceGpsService deviceGpsService;

    @Autowired
    EmqClient emqClient;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        mockGpsData();
    }

    private void mockGpsData() {
        System.out.println(LocalDateTime.now() + "模拟数据产生");
        // 查询所有存在且开启的设备
        QueryWrapper<DeviceStatus> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(DeviceStatus::getStatus, true);
        List<DeviceStatus> list = deviceStatusService.list(wrapper);
        List<Long> collect = list.stream().map(DeviceStatus::getId).collect(Collectors.toList());

        QueryWrapper<Device> wrapper1 = new QueryWrapper<>();
        wrapper1.lambda().gt(Device::getBindTime, 0L)
                .in(Device::getId, collect);

        List<Device> deviceList = deviceService.list(wrapper1);
        deviceList.forEach(device -> {
            Map<String, Object> map = Maps.newHashMap();
            map.put("number", device.getNumber());
            GeoCoordinate randomGps = deviceGpsService.getRandomLocation(device.getNumber());
            Random random = new Random();

            double la = randomGps.getLatitude(), lo = randomGps.getLongitude();
            // 定位到国外自动重置
            if (la < 3.86 || la > 53.55 || lo < 73.66 || lo > 135.05) {
                GeoCoordinate location = deviceGpsService.getRandomLocation(-1L);
                la = location.getLatitude();
                lo = location.getLongitude();
            }
            map.put("lat", la);
            map.put("lon", lo);
            String json = JSON.toJSONString(map);
            emqClient.publish("device/gps", json, 1);
            try {
                Thread.sleep(40 + random.nextInt(100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

}
